//
//  InformationViewController.m
//  RedManStock
//
//  Created by lanou3g on 15/7/25.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "InformationViewController.h"
#import "InformationTableViewCell.h"
#import "MyHandle.h"
#import "InformationDetailVC.h"
#import "MBProgressHUD.h"
@interface InformationViewController ()

@property(nonatomic,retain)NSMutableArray *informationArray;

@end

@implementation InformationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.tableView registerNib:[UINib nibWithNibName:@"InformationTableViewCell" bundle:nil] forCellReuseIdentifier:@"information"];
    self.update = YES;
    
}

//重写数据源的get方法，要不然后面的网数据源添加对象添加不进去
-(NSMutableArray *)informationArray{
    
    if (!_informationArray) {
        _informationArray = [[NSMutableArray alloc]init];
    }
    return _informationArray;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)viewWillAppear:(BOOL)animated{
    
    if (self.update) {
        
        [self loadNewData];
    }
    self.update = NO;
    
}




//http://api.gongniucaijing.com/news/list ?command=1&idfa=7B7C9B42-5C18-4EEB-A0F8-6708DE081AAE&machine=iPhone%205&number=20&offset=20&os=iPhone%20OS%208.2&platform=1&protocolVersion=1.0&qudao=0000001&resolution=640%2A1136&token=d58f02522dac7e698a4600c8b18ff02c0b9fa6df&uuid=BULLSTOCK-79460017-C3E5-45EC-B98C-1DD541A61127&version=1.9.0&versionCode=190

#pragma mark- 下拉刷新 -
-(void)loadNewData{
    
    //    出现大菊花
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSString *urlStr = @"http://api.gongniucaijing.com/news/list";
    [[MyHandle new] requestDataWithURLStr:urlStr parameterBlock:^NSDictionary *{
        NSDictionary *dic = @{@"number":@"20",
                              @"offset":@"0",
                              @"command":@"0",
                              @"token":@"d58f02522dac7e698a4600c8b18ff02c0b9fa6df",
                              @"resolution":@"640%2A1136&token=d58f02522dac7e698a4600c8b18ff02c0b9fa6df"};
        
        return dic;
    } successBlock:^(NSDictionary *responseDic) {
        [self.informationArray removeAllObjects];
        [self dataSourceWithJasonDic:responseDic];
        
    } errorBlock:^(NSError *error) {
        
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"网络连接错误" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        
        [alertView show];
        [self.tableView.header endRefreshing];

        
    }];
    
    [self.tableView.header endRefreshing];
    
    
}

-(void)loadMoreData{
    
    NSString *urlStr = @"http://api.gongniucaijing.com/news/list";
    [[MyHandle new] requestDataWithURLStr:urlStr parameterBlock:^NSDictionary *{
        NSDictionary *dic = @{@"number":@"20",
                              @"offset":[NSString stringWithFormat:@"%lu",(unsigned long)self.informationArray.count],
                              @"command":@"1",
                              @"token":@"d58f02522dac7e698a4600c8b18ff02c0b9fa6df"};
        
        return dic;
    } successBlock:^(NSDictionary *responseDic) {
        [self dataSourceWithJasonDic:responseDic];
        
    } errorBlock:^(NSError *error) {
        
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"网络连接错误" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        
        [alertView show];
        [self.tableView.header endRefreshing];

        
    }];
    
    [self.tableView.footer endRefreshing];

    
}




#pragma mark- 解析，给模型赋值，给数据源赋值， -
-(void)dataSourceWithJasonDic:(NSDictionary *)requestDic{
    
    if (requestDic) {
        
        
        NSDictionary *dataDic = [requestDic objectForKey:@"data"];
        NSArray *listsArray = [dataDic objectForKey:@"lists"];
        for (NSDictionary *dic in listsArray) {
//            注：创建模型必须在for循环里面，否则只有一块内存，最后的对象都是一个对象
            Information *information = [[Information alloc]init];
            [information setValuesForKeysWithDictionary:dic];
            [self.informationArray addObject:information];
            
            
        }
        
        //        结束大菊花
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        [self.tableView reloadData];
        
        
    }else{
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请求失败" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alertView show];
        
        
    }

    
    
    
}




#pragma mark- 分行 -

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.informationArray.count;
}

#pragma mark- 分区 -
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    
    return 1;
}

#pragma mark- cell复用 -
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    InformationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"information"];
    Information *information = [self.informationArray objectAtIndex:indexPath.row];
    cell.information = information;
    
    
    return cell;
    
}


#pragma mark- cell自适应高度 -
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    return 80;
}


#pragma mark- cell被选中方法 -

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    InformationDetailVC *informationDetailVC = [[InformationDetailVC alloc]init];
//    属性传值
   informationDetailVC.information = [self.informationArray objectAtIndex:indexPath.row];
    
    [self.navigationController pushViewController:informationDetailVC animated:YES];
    
}










/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
